package com.xbongbong.saas.script.tempenum;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.paas.field.SerialNumberPoJo;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.SummaryTypeEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.CreditLimitStatusEnum;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CreditLimitEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author xingxing.xiao
 * @date 2020/12/21 15:18
 */
@Getter
public enum TempCreditLimitDistributorEnum {

    BASE_INFO(CreditLimitEnum.BASE_INFO),
    CREDIT_NO(CreditLimitEnum.CREDIT_NO),
    STATUS(CreditLimitEnum.STATUS),
    CUSTOMER_ID(CreditLimitEnum.CUSTOMER_ID),
    FIXED_LIMIT(CreditLimitEnum.FIXED_LIMIT),
    TEMPORARY_LIMIT(CreditLimitEnum.TEMPORARY_LIMIT),
    USED_LIMIT(CreditLimitEnum.USED_LIMIT),
    AVAILABLE_LIMIT(CreditLimitEnum.AVAILABLE_LIMIT),
    SYSTEM_INFO(CreditLimitEnum.SYSTEM_INFO),
    CREATOR_ID(CreditLimitEnum.CREATOR_ID),
    ADD_TIME(CreditLimitEnum.ADD_TIME),
    UPDATE_TIME(CreditLimitEnum.UPDATE_TIME),
    CUSTOMER_NAME(CreditLimitEnum.CUSTOMER_NAME),
    ;

    private CreditLimitEnum anEnum;

    TempCreditLimitDistributorEnum(CreditLimitEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempCreditLimitDistributorEnum> getAllEnum() {
        List<TempCreditLimitDistributorEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    public static void initCreditNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.CREDIT_LIMIT.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.CREDIT_LIMIT.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(CREDIT_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        SerialNumberPoJo serialNumberPoJo = InitExplainsHelper.setSerialNumber(SerialNumberEnum.CREDIT_NO);
        serialNumberPoJo.setResetCycle(0);
        fieldAttrEntity.setSerialNumber(serialNumberPoJo);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setDistributorMark(DistributorMarkEnum.DISTRIBUTOR.getCode());
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    /**
     * 状态
     */
    public static void initStatus(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsDictionary(1);
        List<ItemPoJo> itemPoJos = InitExplainsHelper.setCombItemByEnum(CreditLimitStatusEnum.class, fieldAttrEntity.getFieldType());
        List<String> codeList = Arrays.asList(CreditLimitStatusEnum.FREEZE.getName(), CreditLimitStatusEnum.NORMAL.getName());
        for (ItemPoJo itemPoJo: itemPoJos) {
            itemPoJo.setNoDel(1);
            itemPoJo.setNoEdit(1);
            if (!codeList.contains(itemPoJo.getText())) {
                itemPoJo.setShowType(ShowTypeEnum.NONE.getCode());
                itemPoJo.setEditHide(1);
            } else {
                if (Objects.equals(itemPoJo.getText(), CreditLimitStatusEnum.NORMAL.getName())) {
                    itemPoJo.setChecked(true);
                }
            }
        }
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_OPTION_RELY.getAlias(), ForbiddenSettingEnum.DISABLE_SELF_DEFINE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setItemBanDefault(1);
        fieldAttrEntity.setItems(itemPoJos);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initCustomerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setLinkedText(CUSTOMER_NAME.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setDistributorMark(DistributorMarkEnum.DISTRIBUTOR.getCode());
    }

    public static void initFixedLimit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        DefaultAttrPoJo defaultAttr = fieldAttrEntity.getDefaultAttr();
        defaultAttr.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        defaultAttr.setDefaultValue(0);
        // 默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
    }

    public static void initTemporaryLimit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.NONE.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("生效中的临时额度总和");
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initUsedLimit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.NONE.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("已用额度为所有占用信用之和");
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initAvailableLimit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.NONE.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("可用额度：固定额度 + 临时额度 - 已用额度");
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    /**
     * 不在页面展示
     * @param fieldAttrEntity
     */
    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

}
